package com.bysc.joinservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bysc.baseservice.handler.BaiYunException;
import com.bysc.commonutils.ResultCode;
import com.bysc.commonutils.Results;
import com.bysc.commonutils.constant.MessageConstant;
import com.bysc.commonutils.utils.JwtUtils;
import com.bysc.joinservice.entity.JoinDept;
import com.bysc.joinservice.entity.JoinDeptDetail;
import com.bysc.joinservice.service.JoinDeptDetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

import static com.bysc.commonutils.constant.RedisConstant.*;

@RestController
@RequestMapping("/joinservice/join-dept-detail")
@Api(tags = "部门详情管理")
public class JoinDeptDetailController {

    @Autowired
    private JoinDeptDetailService joinDeptDetailService;

    @ApiOperation(value = "新增部门详情")
    @PostMapping("addDeptDetail")
    public Results addDeptDetail(@ApiParam(name = "DeptDetailInfo", value = "部门详情信息", required = true) @RequestBody JoinDeptDetail deptDetail, HttpServletRequest request) {
        try {
            String TOKEN_clubId= JwtUtils.getClubIdByJwtToken(request);
            if(!StringUtils.isEmpty(TOKEN_clubId)){
                deptDetail.setClubId(TOKEN_clubId);
            }
            joinDeptDetailService.save(deptDetail);
            return Results.ok().message(MessageConstant.ADD_DEPTDETAIL_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.ADD_DEPTDETAIL_FAIL);
        }
    }

    @ApiOperation(value = "根据id修改部门详情信息（后台后端传id）")
    @PutMapping("updateDeptDetailById/{id}")
    public Results updateDeptDetailById(@ApiParam(name = "id", value = "部门详情id", required = true) @PathVariable String id,
                                        @ApiParam(name = "DeptDetailInfo", value = "修改后的部门详情信息", required = true) @RequestBody JoinDeptDetail updatedDeptDetail) {
        try {
            if (updatedDeptDetail != null) {
                joinDeptDetailService.updateById(updatedDeptDetail);
            }
            return Results.ok().message(MessageConstant.EDIT_DEPTDETAIL_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.EDIT_DEPTDETAIL_FAIL);
        }
    }

    @ApiOperation(value = "根据id删除部门详情")
    @DeleteMapping("deleteDeptDetailById/{id}")
    public Results deleteDeptDetailById(@ApiParam(name = "id", value = "部门详情id", required = true) @PathVariable String id) {
            try {
                JoinDeptDetail deptDetail = joinDeptDetailService.getById(id);
                if (deptDetail != null) {
                    joinDeptDetailService.removeById(id);
                }
                return Results.ok().message(MessageConstant.DELETE_DEPTDETAIL_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.DELETE_DEPTDETAIL_FAIL);
        }
    }

    @ApiOperation(value = "根据clubId查找所有部门详情（前端后端适用）")
    @GetMapping("getDeptDetailsByClubId/{clubId}")
    public Results getDeptDetailsByClubId(@PathVariable(required = false) String clubId, HttpServletRequest request) {
        try {
            String TOKEN_clubId = JwtUtils.getClubIdByJwtToken(request);//token解析clubId
            LambdaQueryWrapper<JoinDeptDetail> wrapper = new LambdaQueryWrapper<>();
           if(!StringUtils.isEmpty(TOKEN_clubId)){
               wrapper.eq(JoinDeptDetail::getClubId, TOKEN_clubId);
           }
           else{
               wrapper.eq(JoinDeptDetail::getClubId, clubId);
           }
            List<JoinDeptDetail> deptDetails = joinDeptDetailService.list(wrapper);
            return Results.ok().data("deptDetails", deptDetails).message(MessageConstant.QUERY_DEPTDETAIL_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_DEPTDETAIL_FAIL);
        }
    }


    @ApiOperation(value = "根据部门id查询部门细节")
    @PostMapping("getDeptDetailByDeptId/{deptId}")
    public Results getDeptDetailByDeptName(@PathVariable String deptId){
        try {
            LambdaQueryWrapper<JoinDeptDetail> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(JoinDeptDetail::getDeptId, deptId);
            JoinDeptDetail deptDetail = joinDeptDetailService.getOne(wrapper);
            if(deptDetail==null){
                return Results.ok().message(MessageConstant.QUERY_DEPTDETAIL_FAIL);
            }
            return Results.ok().data("deptDetail", deptDetail).message(MessageConstant.QUERY_DEPTDETAIL_SUCCESS);
        } catch (Exception e) {
            throw new BaiYunException(ResultCode.ERROR, MessageConstant.QUERY_DEPTDETAIL_FAIL);
        }

    }

}